<%@page import="com.crm.entity.Parts,com.crm.entity.Sources,java.util.*" %>
<%@page import="com.crm.Business.CompareSrcVO" %>
<%List<Parts> partslist =(List<Parts>)request.getAttribute("partslist");%>
<%List<CompareSrcVO> listcompare =(List<CompareSrcVO>)request.getAttribute("lscompare");%>
<script type="text/javascript">
$(function(){
	  $("#compareSourcesTable").colResizable();
	});
</script>
<table id="compareSourcesTable" width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#C5E4EB">
          <tr>
            <td width="20" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"  style="background-repeat:repeat-x">&nbsp;</td>
            <td width="33" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF"><span style="background-repeat:repeat-x">S.No</span></td>
            <td width="40" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x";bgcolor="#FFFFFF"> Part No </td>
            <td width="40" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x";bgcolor="#FFFFFF"> Description</td>
             <td width="40" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">OffPartNo </td>
            <td width="42" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"  style="background-repeat:repeat-x" bgcolor="#FFFFFF">Required Qty </td>
            <td width="67" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">Source</td>
            <td width="74" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">Supplier</td>
            <td width="59" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">Unit Price </td>
            <td width="59" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">Currency</td>
            <td width="80" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-x" bgcolor="#FFFFFF">Qty availble</td>
            <td width="76" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:rrepeat-x" bgcolor="#FFFFFF">M.O.Q</td>
             <td width="76" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">S.P.Q</td>
            <td width="81" align="left" valign="middle" background="<%=application.getContextPath() %>/images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">Conditions</td>
          </tr>
             <% if(listcompare!=null){
        	   for(int i=0;i<listcompare.size();i++){  %>
          <tr>
         
            <td bgcolor="#FFFFFF">
            <input name="autocheck<%=i %>" id="checkid<%=i %>" type="checkbox"   value="<%=i %>"class="content"  />
            <input type="hidden" name="columnCount" value="<%=listcompare.size()%>" id="columnCount"/> 
            </td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="sno<%=i %>" id="sno<%=i %>" type="text" value="<%=listcompare.get(i).getSno() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input  class="part" name="partno<%=i %>" id="partno<%=i %>" type="text" value="<%=listcompare.get(i).getPartNum() %>"  size="5"/><input  name="partid<%=i %>" id="partid<%=i %>" type="hidden" value="<%=listcompare.get(i).getPartid() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input  name="desc<%=i %>" id="desc<%=i %>" type="text" value="<%=listcompare.get(i).getDesc() %>"  size="5"/>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="offpartno<%=i %>" id="offpartno<%=i %>" type="text" value="<%=listcompare.get(i).getOffPartNo() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="rqty<%=i %>" id="rqty<%=i %>" type="text" value="<%=listcompare.get(i).getRequiredQty() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="source<%=i %>" id="source<%=i %>" type="text" value="<%=listcompare.get(i).getSourceName() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="supplier<%=i %>" id="supplier<%=i %>" type="text" value="<%=listcompare.get(i).getSupplierName() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="uprice<%=i %>" id="uprice<%=i %>"  type="text" value="<%=listcompare.get(i).getUprice() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="currency<%=i %>" id="currency<%=i %>"  type="text" value="<%=listcompare.get(i).getSourceCurrency() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="qtyavail<%=i %>" id="qtyavail<%=i %>" type="text" value="<%=listcompare.get(i).getAvailbleQty() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="moq<%=i %>"  id="moq<%=i %>" type="text" value="<%=listcompare.get(i).getMoq() %>"  size="5"/></td>
             <td align="left" valign="top" bgcolor="#FFFFFF"><input name="spq<%=i %>"  id="spq<%=i %>" type="text" value="<%=listcompare.get(i).getStdpackqty() %>"  size="5"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF"><input name="conditions<%=i %>" id="conditions<%=i %>" type="text" value="<%=listcompare.get(i).getConditions() %>"  size="5"/></td>
          </tr>
         <%}
         }%>
        </table>
        <div class="box2">
	    <div class="buttton-main">
	      <input type="submit" class="button" onclick="javascript:return  validateExpo()" value="Export"/><!--
	      <input type="button" class="button" onclick="javascript:validateExpo()" value="validate"/> 
	      --><input type="button" id="autocompareid" class="button" value="Auto Compare" onclick="javascript:autoCompare()"/></div>
	  </div>
	  <script type="text/javascript">  
	  $(function(){
			 var client="<%=request.getAttribute("clientdet")%>";
			 var cldiv="<%=request.getAttribute("division")%>";
			 var cluser="<%=request.getAttribute("user")%>";
			 $("#enqclient").html("Client Name Is:<span class='cliendet'>"+client +
			"</span>   &nbsp;&nbsp;&nbsp;&nbsp;  Client Division Is:<span class='cliendet'>"+cldiv+
			 "</span>    &nbsp;&nbsp;&nbsp;&nbsp;    Client User Is:<span class='cliendet'>"+cluser+"</span>");
			 $("#enqclient").show();
		});
function autoCompare(){
//alert($('input:checkbox').prop('checked',false));
//$('input:checkbox').attr('checked', false); 
	//$('input:checkbox:checked').removeAttr('checked');
	var count=$("#columnCount").val();
	var loweruprice=0;
	var comparepart="";
	var descript="";
	var varcount=0;
	
	for(var i=0;i<count;i++)
		{
	var partname=$("#partno"+i).val();
	var desc=$("#desc"+i).val();
	var req_qty=$("#rqty"+i).val();
	var offpartname=$("#offpartno"+i).val();
	var uprice=$("#uprice"+i).val();
	var spq=$("#spq"+i).val();
	var moq=$("#moq"+i).val();
	var checked=$("#checkid"+i).val();
	moq=parseInt(moq);
	req_qty=parseInt(req_qty);
		if((partname==offpartname)&&(moq<=req_qty)&&(req_qty%spq==0)){
	//Checking for Single part which satisfys above condition
			if(comparepart!=partname)
		{ 
		descript=desc;
		comparepart=partname;
		varcount=i;
		loweruprice=uprice;
		$("#checkid"+i).prop('checked', 'true');//("checked","checked");
			
	}
	else{
		if(descript!=desc){
			descript=desc;
			comparepart=partname;
			varcount=i;
			loweruprice=uprice;
			$("#checkid"+i).prop('checked', 'true');//attr("checked","checked");
		}else {
	if(loweruprice>=uprice)
	{
		loweruprice=uprice;
		varcount=i;
		$("#checkid"+(i-1)).prop('checked', '');//removeAttr('checked');
		$("#checkid"+i).prop('checked', 'true');//attr("checked","checked");
	}
	}
	}

	
	}
	
	}
}
function validateExpo(){
	var count=$("#columnCount").val();
	 var partarr= new Array();
	var loweruprice=0;
	var comparepart="";
	var varcount=0;
	var checkcount=0;
	for(var i=0;i<count;i++)
		{
		var isChecked = $('#checkid'+i).prop('checked')?true:false;
		if(isChecked==1)
			{ 
			partarr.push($("#partid"+i).val());
			//alert("the CheckBox is Checked");
			checkcount++;
			}
}
	if(partarr.length<1)
	{
jAlert("Plz Check Aleast One Part");
return false;
}
// sorting array in the ascending oreder
	var sorted_arr= partarr.sort(); 
   var results = [];
for (var i = 0; i <partarr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
if(results.length>0)
	{
jAlert("Duplicate Parts Are Selected ");
return false;
}
else{return myFunction();}
}
</script>
<script type="text/javascript">
function myFunction()
{
var x;
var r=confirm("Make Sure That Duplicate Parts Are Not Selected");
if (r==true)
  {
return true;
  }
else
  {
 return false;
  }

}
</script>


	  